package com.quo.controller;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quo.dto.QuotesDto;
import com.quo.entity.ProductCount;
import com.quo.entity.ProductSeries2;
import com.quo.entity.Quote;
import com.quo.service.ProductService;
import com.quo.service.QuoteService;
import com.quo.utils.Result;
import com.quo.utils.ResultCode;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;



/** 

* @author zhoumin

*/
@RestController
@RequestMapping(value="/api")
public class JasperController {
	
	@Autowired
	public QuoteService qService;
	@Autowired
	public ProductService pService;
	@PostMapping("/quote-all-print")
	 public void createPdfList(@RequestBody Long[] qids,HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.引入jasper文件
		/*Resource resource = new ClassPathResource("jasper/quoteList.jasper");
		FileInputStream fis = new FileInputStream(resource.getFile());*/
		InputStream fis = getClass().getResourceAsStream("/jasper/quoteList.jasper");

        //2.创建JasperPrint,向jasper文件中填充数据
        ServletOutputStream os = response.getOutputStream();
        try {
                    Map parameters = new HashMap<>();
            			 
            	parameters.put("qids",qids);  
            //获取数据库连接
            Connection conn= getConnection();
            JasperPrint print = JasperFillManager.fillReport(fis,parameters,conn);
            JasperExportManager.exportReportToPdfStream(print,os);
        } catch (JRException e) {
            e.printStackTrace();
        }finally {
            os.flush();
        }
    }
	
	@PostMapping("/quote-all-print2")
	 public void createPdf3(@RequestBody Long[] qids,HttpServletRequest request, HttpServletResponse response) throws Exception {
       //1.引入jasper文件
		/*Resource resource = new ClassPathResource("jasper/quoteList2.jasper");
		FileInputStream fis = new FileInputStream(resource.getFile());*/
		InputStream fis = getClass().getResourceAsStream("/jasper/quoteList2.jasper");
       //2.创建JasperPrint,向jasper文件中填充数据
       ServletOutputStream os = response.getOutputStream();
       try {
                   Map parameters = new HashMap<>();
           			 
           	parameters.put("qids",qids);  
           //获取数据库连接
           Connection conn= getConnection();
           JasperPrint print = JasperFillManager.fillReport(fis,parameters,conn);
           JasperExportManager.exportReportToPdfStream(print,os);
       } catch (JRException e) {
           e.printStackTrace();
       }finally {
           os.flush();
       }
   }
	
	@GetMapping("/quote-all-print/{qid}")
	 public void createPdf(@PathVariable Long qid,HttpServletRequest request, HttpServletResponse response) throws Exception {
       //1.引入jasper文件
		/*Resource resource = new ClassPathResource("jasper/quote.jasper");
		FileInputStream fis = new FileInputStream(resource.getFile());*/
		InputStream fis = getClass().getResourceAsStream("/jasper/quote.jasper");

       //2.创建JasperPrint,向jasper文件中填充数据
       ServletOutputStream os = response.getOutputStream();
       try {
           Map parameters = new HashMap<>();
           parameters.put("qid",qid);

           //获取数据库连接
           Connection conn= getConnection();
           JasperPrint print = JasperFillManager.fillReport(fis,parameters,conn);
           JasperExportManager.exportReportToPdfStream(print,os);
       } catch (JRException e) {
           e.printStackTrace();
       }finally {
           os.flush();
       }
   }


	@GetMapping("/order-all-print/{qid}")
	 public void createOrderPdf2(@PathVariable Long qid,HttpServletRequest request, HttpServletResponse response) throws Exception {
       System.out.println("aaa");
		//1.引入jasper文件
       // Resource resource = new ClassPathResource("jasper/order2.jasper");
       InputStream fis = getClass().getResourceAsStream("/jasper/order2.jasper");

       // FileInputStream fis = new FileInputStream(resource.getFile());

       //2.创建JasperPrint,向jasper文件中填充数据
       ServletOutputStream os = response.getOutputStream();
       try {
           Map parameters = new HashMap<>();
           parameters.put("qid",qid);

           //获取数据库连接
           Connection conn= getConnection();
           JasperPrint print = JasperFillManager.fillReport(fis,parameters,conn);
           JasperExportManager.exportReportToPdfStream(print,os);
       } catch (JRException e) {
           e.printStackTrace();
       }finally {
           os.flush();
       }
   }
	
	@GetMapping("/createCharts")
	 public void createChartsPdf2(HttpServletRequest request, HttpServletResponse response) throws Exception {
      System.out.println("aaa");
		//1.引入jasper文件
      // Resource resource = new ClassPathResource("jasper/order2.jasper");
      InputStream fis = getClass().getResourceAsStream("/jasper/TestCharts.jasper");

      // FileInputStream fis = new FileInputStream(resource.getFile());

      //2.创建JasperPrint,向jasper文件中填充数据
      ServletOutputStream os = response.getOutputStream();
      try {
          Map parameters = new HashMap<>();

          //获取数据库连接
          Connection conn= getConnection();
          JasperPrint print = JasperFillManager.fillReport(fis,parameters,conn);
          JasperExportManager.exportReportToPdfStream(print,os);
      } catch (JRException e) {
          e.printStackTrace();
      }finally {
          os.flush();
      }
  }
	
    public Connection getConnection() throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@82.157.249.88:1521/xe","tofu15","LxUaqVk5bNobMGLyDwnY");
        return conn;
    }
    
 
//        /**
//         *  charts图形报表
//         */
//        @GetMapping("/testJasper")
//        public void createChartsPdf(HttpServletRequest request, HttpServletResponse response) throws Exception {
//            //1.引入jasper文件
//            Resource resource = new ClassPathResource("jasper/TestCharts.jasper");
//            FileInputStream fis = new FileInputStream(resource.getFile());
//
//            //2.创建JasperPrint,向jasper文件中填充数据
//            ServletOutputStream os = response.getOutputStream();
//            try {
//                Map parameters = new HashMap<>();
//                //构建javaBean的数据源
//                //1.获取到对象的list集合
//                List<ProductCount> userList = getProductCountList();
//                //2.通过list集合创建javaBean的数据源对象
//                JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(userList);
//                JasperPrint print = JasperFillManager.fillReport(fis,parameters,ds);
//                JasperExportManager.exportReportToPdfStream(print,os);
//            } catch (JRException e) {
//                e.printStackTrace();
//            }finally {
//                os.flush();
//            }
//        }
//
//        public List<ProductCount> getProductCountList() {
//            List<ProductCount> Countlist = pService.getProductCountList();
//            return Countlist;
//        }


    }

    

